package br.com.bco.web.page.motorista;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.bco.controle.funcionario.MotoristaControl;
import br.com.bco.negocio.Motorista;
import br.com.bco.negocio.exception.NegocioException;
import br.com.bco.web.page.AbstractPage;

public class InserirMotorista extends AbstractPage {
    private final Motorista motorista = new Motorista();

    private TextField nome;
    private TextField cpf;
    private TextField cnh;
    private TextField matricula;
    private TextField dtu;
    private TextField horaEntrada;
    private Label lblNome;
    private Label lblCPF;
    private Label lblCNH;
    private Label lblMatricula;
    private Label lblDTU;
    private Label lblHoraEntrada;

    @SpringBean
    private MotoristaControl motoristaControl;

    public InserirMotorista() {
        Form form = new Form("form", new CompoundPropertyModel(motorista));
        adicionarCampos(form);
        adicionarBotoes(form);
        add(form);
    }

    private void adicionarCampos(Form form) {
        nome = new TextField("nome");
        cpf = new TextField("cpf");
        cnh = new TextField("cnh");
        matricula = new TextField("matricula");
        dtu = new TextField("dtu");
        horaEntrada = new TextField("horaEntrada");
        lblNome = new Label("lblNome", "Nome");
        lblCPF = new Label("lblCPF", "CPF");
        lblCNH = new Label("lblCNH", "CNH");
        lblMatricula = new Label("lblMatricula", "Matricula");
        lblDTU = new Label("lblDTU", "DTU");
        lblHoraEntrada = new Label("lblHoraEntrada", "HoraEntrada");
        form.add(nome);
        form.add(cpf);
        form.add(cnh);
        form.add(matricula);
        form.add(dtu);
        form.add(horaEntrada);
        form.add(lblNome);
        form.add(lblCPF);
        form.add(lblCNH);
        form.add(lblMatricula);
        form.add(lblDTU);
        form.add(lblHoraEntrada);
    }

    private void adicionarBotoes(Form form) {
        Button btnGravar = new Button("btnGravar", new Model()) {
            @Override
            public void onSubmit() {
                try {
                    motoristaControl.inserir(motorista);
                } catch (NegocioException e) {
                    System.out.println(e.getMensagens().get(0));
                }
                setResponsePage(InserirMotorista.class);
            }
        };
        form.add(btnGravar);
    }

    @Override
    public String getTitulo() {
        return "Inserir Motorista";
    }
}
